// CSE 142 Winter 2008, Marty Stepp // // This program checks whether two words rhyme (end with the same 2 letters) // or alliterate (start with the same letter) using String objects. // // This is an updated version of a program from a previous lecture, modified // to use boolean methods for whether two words rhyme or alliterate. // (Look at how clean and readable the main method has become!) import java.util.*; // for Scanner public class Rhyme2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type two words: "); String word1 = console.next(); String word2 = console.next(); if (rhymes(word1, word2)) { System.out.println("They rhyme!"); } if (alliterates(word1, word2)) { System.out.println("They alliterate!"); } } // Returns true if the given two words end with the same last two letters, otherwise false. public static boolean rhymes(String s1, String s2) { String last2 = s1.substring(s1.length() - 2); if (s2.endsWith(last2)) { return true; } else { return false; } } // Returns true if the given two words start with the same letter, otherwise false. public static boolean alliterates(String s1, String s2) { String first = s1.substring(0, 1); if (s2.startsWith(first)) { return true; } else { return false; } } }